from django.db import models
from django.db import connection

import logging

class Entry(models.Model):
    name = models.CharField(max_length=100)
    created_at = models.DateTimeField(auto_now_add=True)
    hits = models.IntegerField(default=0)

    def __unicode__(self):
        return self.name
    
    @models.permalink
    def get_absolute_url(self):
        return ('entry_details', (), {
            'entry_id': self.id,
        })

    def hit(self, hits_no=1):
        logging.debug("Hitting entry %s x %d" % (self, hits_no))
        self.__class__.objects\
            .filter(id=self.id)\
            .update(hits=models.F('hits') + hits_no)
        logging.sql(connection.queries[-1]['sql'])

